package com.c2soft.ncmxTrade.controller;

import java.net.URI;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.log4j.Log4j2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.UriComponentsBuilder;

import com.c2soft.ncmxTrade.command.OrderCommand;
import com.c2soft.ncmxTrade.util.JsonUtil;

@Log4j2
@Controller
@RequestMapping("/order")
public class OrderMgmtController {
	
	@Value("#{systemProperties['ncmxServer.redirect.url']}")
	private String baseServerUrl;
	
	@Autowired
	private RestTemplate restTemplate;
	
	@RequestMapping(value = "/show")
	public String show() {
		return "testOrder";
	}
	
	
	
	
	@RequestMapping(value = "/buyOrder", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json")
	@ResponseBody
	public String buy(@RequestBody OrderCommand buy,HttpServletRequest req) {
		buy.setUserId((String) req.getSession().getAttribute("trdUsrId"));
		
		URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
			    .path("order/buy")
			  
			    .queryParam("otype", buy.getOtype())
			   .queryParam("insname", buy.getInsname())
			    .queryParam("symbol", buy.getSymbol())
			    .queryParam("expdate", buy.getExpdate())
			    .queryParam("qt", buy.getQt())
			    .queryParam("ql", buy.getQl())
			    .queryParam("qw", buy.getQw())
			    .queryParam("price", buy.getPrice())
			    .queryParam("ptype", buy.getPtype())
			    .queryParam("status", buy.getStatus())
			    .queryParam("tp", buy.getTp())
			    .queryParam("userId", buy.getUserId())
			    .queryParam("accno", buy.getAccno())
			    .queryParam("cashl", buy.getCashl())
			    .queryParam("marginl", buy.getMarginl())
			    .queryParam("margin_b", buy.getMargin_b())
			    .build()
			    .toUri();
		
		String response=restTemplate.getForObject(redirectUrl, String.class);
		String[] appendArray={"{\"rows\":",response.toString(),"}"};//TODO: clean junk
			return response;
		
	}
	
	@RequestMapping(value = "/sellOrder", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json")
	
	public @ResponseBody String sell(@RequestBody OrderCommand sell,HttpServletRequest req) {
		
		sell.setUserId((String) req.getSession().getAttribute("trdUsrId"));
		URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
			    .path("order/sell")
			  
			    .queryParam("otype", sell.getOtype())
			   .queryParam("insname", sell.getInsname())
			    .queryParam("symbol", sell.getSymbol())
			    .queryParam("expdate", sell.getExpdate())
			    .queryParam("qt", sell.getQt())
			    .queryParam("ql", sell.getQl())
			    .queryParam("qw", sell.getQw())
			    .queryParam("price", sell.getPrice())
			    .queryParam("ptype", sell.getPtype())
			    .queryParam("status", sell.getStatus())
			    .queryParam("tp", sell.getTp())
			    .queryParam("userId", sell.getUserId())
			    .queryParam("accno", sell.getAccno())
			    .queryParam("cashl", sell.getCashl())
			    .queryParam("marginl", sell.getMarginl())
			    .queryParam("margin_b", sell.getMargin_b())
			    .build()
			    .toUri();
		
		System.out.println("Sell Redirect URL: "+redirectUrl);	
		String response=restTemplate.getForObject(redirectUrl, String.class);
		String[] appendArray={"{\"rows\":",response.toString(),"}"};//TODO: clean junk
			return response;
			
		
		}	
	

	@RequestMapping(value = "/buyDetail",method=RequestMethod.GET)
		public @ResponseBody List buyDetail(HttpServletRequest req) {
		
		String userId=(String) req.getSession().getAttribute("trdUsrId");
		URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
			    .path("order/buyorderMgmt")
			    .queryParam("userId", userId)
			    .build()
			    .toUri();
		
		
		List response=restTemplate.getForObject(redirectUrl, List.class);
		
		String[] appendArray={"{\"rows\":",response.toString(),"}"};//TODO: clean junk
		return response;
	}
	
	
}
